<style>
body {
  background-image: url("../../public/loginImage.png");
  background-size: cover; /*设置封面*/
}

.layout-header {
  background: rgba(255, 255, 255, 0.5);
  color: #fff;
  text-align: left;
  line-height: 80px !important;
}

a {
  text-decoration: none;
  font-size: 25px;
}

.layout-main {
  text-align: center;
}

/*表单字体*/
.el-form-item__label {
  color: white;
  font-size: 16px;
}

/*分割线*/
.el-divider--horizontal {
  width: 76%;
  margin: 24px 600px;
}

.el-divider__text {
  font-size: 23px;
  font-family: 华文中宋;
  background-color: #ffd300;
}
</style>
<template>
  <div>
    <el-container>
      <el-header class="layout-header">
        <div class="block">
          <h1 style="color: #223741;font-size: 35px;font-family: 华文中宋;font-weight: bold">皖派商城
            <span style="float: right">
              <a style="font-size: 25px" href="/adminLogin">Admin Login</a>&nbsp/
              <a style="font-size: 25px" href="/register">Sign Up</a>
          </span>
          </h1>
        </div>
      </el-header>
      <el-main class="layout-main">
        <span style="color: white;font-size: 45px;font-family: 华文中宋;font-weight: bold">为了无法计算的价值!</span>
        <el-divider content-position="right" style="font-size: 20px">阿里云</el-divider>
        <el-card style="width: 560px;height: 420px;margin: 300px 1400px;
                background-color: rgba(255,255,255,0.3)">
          <!--label-width设置用户名这一列所占的宽度,如果不设置会显示在上面-->
          <h1 style="font-family: 华文中宋;font-weight: bold;font-size: 30px;color: white">用户登录</h1>
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px"
                   style="width: 400px;margin: 40px auto">
            <el-form-item label="用户名" prop="username">
              <el-input type="username" v-model="ruleForm.username" placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input type="password" v-model="ruleForm.password" placeholder="请输入密码"></el-input>
            </el-form-item>
            <el-form-item>
              <el-checkbox v-model="ruleForm.rem" style="color: white">记住用户名和密码</el-checkbox>
              <!--rem默认为false,勾选后为true-->
            </el-form-item>
            <el-form-item>
              <el-button style="position: relative;right: 30px"
                         type="primary" @click="submitForm('ruleForm')">登录
              </el-button>
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-main>
    </el-container>
  </div>
</template>
<script>

export default {
  data() {
    return {
      ruleForm: {// 初始化ruleForm对象
        username: '',
        password: '',
        rem: false//rem默认为false,勾选后为true
      },
      rules: { // 制定规则
        username: [
          {required: true, message: '请输入账号', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
      },
    };
  },
  methods: {
    open1() {
      const h = this.$createElement;
      this.$notify({
        title: 'Welcome!',
        message: h('i', {style: 'color: green'}, 'Welcome to Wanpai Mall. Please log in first and start your new shopping trip!'),
        showClose: true
      });
    },
    // 处理登录事件
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = this.GLOBAL.productUrl + 'users/login';
          let formData = this.qs.stringify(this.ruleForm)
          this.axios.post(url, formData).then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              let ruleFormString = JSON.stringify(this.ruleForm.username);
              localStorage.setItem('ruleFormToUser', ruleFormString);
              location.href = "/index";
              let jwt = responseBody.data;
              console.log('登陆成功,服务器响应JWT:' + jwt);
              localStorage.setItem('jwtToUser', jwt);
              console.log('已经将JWT保存到localStorage中')
              if (this.ruleForm.rem) {
                localStorage.setItem("usernameToUser", this.ruleForm.username);
                localStorage.setItem("passwordToUser", this.ruleForm.password);
              } else {
                localStorage.removeItem("usernameToUser");
                localStorage.removeItem("passwordToUser");
              }
              this.$message({
                message: '登录成功,欢迎回来!',
                type: 'success'
              });
            } else {
              this.$message.error(responseBody.message);
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    let username = localStorage.getItem('usernameToUser');
    let password = localStorage.getItem('passwordToUser');
    if (username != '' && password != '') {
      this.ruleForm.username = username;
      this.ruleForm.password = password;
    }
    this.open1();
  },
}
</script>